#include "main.h"

#include "Hook_Scaleform.h"
#include "PluginAPI.h"
#include "Relocation/RVA.h"


SFSEMessagingInterface* g_messaging = nullptr;
SFSEMenuInterface* g_menu = nullptr;

#include "inih/cpp/INIReader.h"

std::set<std::string> menus;
std::string sFont = "$MAIN_Font";
u32 iX = 1800;
u32 iY = 1050;
u32 iFontSize = 21;
bool bShowSeconds = false;
bool b12hFormat = true;

void MessageCallback(SFSEMessagingInterface::Message* msg)
{
    switch (msg->type)
    {
    case SFSEMessagingInterface::kMessage_PostPostLoad:
        _MESSAGE("postpostload");
        Hooks_Scaleform_Apply();
        //Hooks_Scaleform_Commit();
        INIReader reader("./Data/MCM/Settings/DEF_TOLS.ini");
        if (!reader.ParseError())
        {
            iX = reader.GetInteger("Main", "iX", 1800);
            iY = reader.GetInteger("Main", "iY", 1050);
            iFontSize = reader.GetInteger("Main", "iFontSize", 21);
            sFont = reader.Get("Main", "sFont", "$MAIN_Font");
            bShowSeconds = reader.GetBoolean("Main", "bShowSeconds", false);
            b12hFormat = reader.GetBoolean("Main", "b12hFormat", true);
            auto sections = reader.GetSections();
            for (auto f : sections)
            {
                if (!_strcmpi(f.c_str(), "Menus"))
                {
                    auto fields = reader.GetFields(f);
                    for (auto g : fields)
                    {
                        if (reader.GetBoolean(f, g, false))
                        {
                            menus.insert(g.substr(1));
                        }
                    }
                    break;
                }
            }
        }

        break;
    }
}


extern "C" {
__declspec(dllexport) SFSEPluginVersionData SFSEPlugin_Version =
{
    SFSEPluginVersionData::kVersion,
    1,
    "DEF_TOLS",
    "Neanka",
    0, // not address independent
    0, // not structure independent
    {CURRENT_RELEASE_RUNTIME, 0}, // compatible with 1.7.23 and that's it
    1, // works with any version of the script extender. you probably do not need to put anything here
    0, 0, // set these reserved fields to 0
};

__declspec(dllexport) bool SFSEPlugin_Load(const SFSEInterface* sfse)
{
    DebugLog::openRelative(CSIDL_MYDOCUMENTS, "\\My Games\\Starfield\\SFSE\\Logs\\DEF_TOLS.txt");
    _MESSAGE("load");

    g_messaging = static_cast<SFSEMessagingInterface*>(sfse->QueryInterface(kInterface_Messaging));
    if (!g_messaging)
    {
        _FATALERROR("couldn't get messaging interface");
        return false;
    }
    g_messaging->RegisterListener(sfse->GetPluginHandle(), "SFSE", MessageCallback);
    g_menu = static_cast<SFSEMenuInterface*>(sfse->QueryInterface(kInterface_Menu));
    if (!g_menu)
    {
        _FATALERROR("couldn't get menu interface");
        return false;
    }
    g_menu->Register(MenuCallback);

    if (!g_branchTrampoline.create(1024 * 64))
    {
        _ERROR("couldn't create branch trampoline. this is fatal. skipping remainder of init process.");
        return false;
    }

    if (!g_localTrampoline.create(1024 * 64, nullptr))
    {
        _ERROR("couldn't create codegen buffer. this is fatal. skipping remainder of init process.");
        return false;
    }

    // sig scan 
    //plugin_info.plugin_name = "DEF_TOLS";
    //plugin_info.runtime_version = CURRENT_RELEASE_RUNTIME;
    //sig_scan_timer tmr;
    //tmr.start();
    //RELOC_GLOBAL_VAL(MovieDefImpl_CreateInstance, "48 89 5C 24 10 48 89 6C 24 18 48 89 74 24 20 57 48 83 EC 30 48 8B 01 49 8B F1")
    //RELOC_GLOBAL_VAL(g_player, "83 FE 02 0F 85 ? ? ? ? EB ?", 0x17, 3, 7)
    //_MESSAGE("Sigscan elapsed: %llu ms.", tmr.stop());

    return true;
}
};
